package main

import (
	. "CrudProject/dao"
	. "CrudProject/validator"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/validator/v10"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

func main() {
	//Router.Router()

	router := gin.Default()
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		v.RegisterValidation("topicUrl", TopicUrl)
	}

	/*
		router.GET("/v1/topic", func(c *gin.Context) {
			if c.Query("username") ==""{
				c.String(http.StatusOK,"获取帖子列表")
			}else {
				username :=c.Query("username")
				fmt.Print("username= ",username)
				c.String(200,"获取用户名为%s的帖子的列表",c.Query("username"))
			}
		})

		router.GET("/v1/topic/:topic_id", func(c *gin.Context) {
			c.String(200,"获取topic id=%s的帖子",c.Param("topic_id"))
		})

		router.Run()*/

	//路由设置
	v1 := router.Group("/a1/topic")
	{
		v1.GET("", func(c *gin.Context) {
			if c.Query("username") == "" {
				c.String(200, "获取帖子列表")
			} else {
				c.String(200, "获取用户名为%s的帖子的列表", c.Query("username"))
			}
		})
		v1.GET("/:topic_id", GetTopicDetail)
		router.GET("v2/topic/list", GetTopicList)

		//v1.Use(MustLogin()) //dao层定义的返回值是HandlerFunc，所以这里必须要加括号

		v1.POST("", AddTopic)
		v1.DELETE("/:id", DeleteTopic)
	}

	v2 := router.Group("/a2/topic")
	{
		v2.POST("", AddModelTopic)
	}

	router.Run()
}
